/**
 * 定义方法calculate(), 接收两个int类型的整数,
 * 分别获取这两个整数的加减乘除的运算结果, 然后返回.
 * 最后在main方法中调用该方法, 并打印结果.
 */
public class Demo08 {
    public static void main(String[] args) {
        int x = 30, y = 20;
        //策略模式 结果测试
        Calculator calculator = new Calculator(new AddOperation());
        System.out.println("30+20=" + calculator.calculate(x, y));
        calculator.setOperation(new SubOperation());
        System.out.println("30-20=" + calculator.calculate(x, y));
        calculator.setOperation(new MulOperation());
        System.out.println("30*20=" + calculator.calculate(x, y));
        calculator.setOperation(new DivOperation());
        System.out.println("30/20=" + calculator.calculate(x, y));
    }
}

/**
 * 策略四除法运算
 */
class DivOperation implements Operation {
    public int calculate(int x, int y) {
        return x / y;
    }
}

/**
 * 策略三乘法运算
 */
class MulOperation implements Operation {
    public int calculate(int x, int y) {
        return x * y;
    }
}

/**
 * 策略二减法运算
 */
class SubOperation implements Operation {
    public int calculate(int x, int y) {
        return x - y;
    }
}

/**
 * 策略一加法运算
 */
class AddOperation implements Operation {
    public int calculate(int x, int y) {
        return x + y;
    }
}

/**
 * 定义一个操作的接口
 * 提供两个数字，给出这两个数字的加减乘除的结果
 */
interface Operation {
    int calculate(int x, int y);
}

/**
 * 定义一个Calculator类.
 */
class Calculator {
    private Operation operation;

    public Calculator(Operation operation) {
        this.operation = operation;
    }

    public void setOperation(Operation operation) {
        this.operation = operation;
    }

    public int calculate(int x, int y) {
        return operation.calculate(x, y);
    }
}